from django.contrib import admin
from django import forms
from models import Item
from prettypony.widgets import AdminImageFieldWithThumbWidget

class ItemAdmin(admin.ModelAdmin):
    list_display = ('thumbnail', '__unicode__', 'visit',)

    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(ItemAdmin,self).formfield_for_dbfield(db_field,**kwargs)
        if db_field.name == 'image':
           return forms.ImageField(widget=AdminImageFieldWithThumbWidget())
        return field

    def visit(self, obj):
        """Display a link to the item."""
        return ('<a href="%s">%s</a>' % (obj.url, obj.url,))
    visit.allow_tags = True

    def thumbnail(self, obj):
        """Show the automatic sorl-thumbnail."""
        return ("%s" % (obj.image.thumbnail_tag,))
    thumbnail.allow_tags = True

admin.site.register(Item, ItemAdmin)
